//
//  RSHomeTableViewCell.swift
//  RSWeibo
//
//  Created by Macx on 16/8/12.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit
import SnapKit

class RSStatusViewCell: UITableViewCell {
    
    // 定义一个约束的属性, 用来在判断是否有转发微博的判断中修改底部按钮视图的约束
    var bottomConstraint: Constraint?
    
    // 设置数据源
    var statusViewModel: RSStatusViewModel? {
        didSet{
        
            // 原创微博赋值
            originalView.statusViewModel = statusViewModel
            // 转发微博
            retweetView.retweetViewModel = statusViewModel
            // 底部按钮
            bottomView.statusViewModel = statusViewModel
            
            
            // 先把原来的约束卸载掉
            self.bottomConstraint?.uninstall()
            
            // 判断是否有转发微博, 没有的话得修改约束, 把底部按钮视图的约束进行修改
            if statusViewModel!.statusModel?.retweeted_status != nil{
                
                // 有转发微博, 把转发微博视图隐藏取消, 就把约束更新成这样
                retweetView.hidden = false
                bottomView.snp_makeConstraints(closure: { (make) in
                    self.bottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
                
                // 法二, 这也是可以的
//                bottomView.snp_remakeConstraints(closure: { (make) in
//                    make.left.equalTo(contentView)
//                    make.top.equalTo(retweetView.snp_bottom).constraint
//                    make.right.equalTo(contentView)
//                    make.height.equalTo(30)
//                })
             
            
                
            }else{
                
                // 没有转发微博, 把转发微博隐藏,就把约束改成这样
                retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) in
                    self.bottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint

                })
                
                // 法二, 这也是可以的
//                bottomView.snp_remakeConstraints(closure: { (make) in
//                    make.left.equalTo(contentView)
//                    make.top.equalTo(originalView.snp_bottom).constraint
//                    make.right.equalTo(contentView)
//                    make.height.equalTo(30)
//                })

                
                
   
            }
            
            
        }
    
    }
    
    
    
    /*
     拥有三个属性控件
     1,原创微博 RSOriginalView
     2,转发微博
     3,最下面的按钮栏
     
     */
    
    //  1,原创微博 RSOriginalView
    private lazy var originalView: RSOriginalView = RSOriginalView()
    
    // 2, 转发微博 RSRetweetView
    private lazy var retweetView: RSRetweetView = RSRetweetView()
    
    // 3,底部按钮
    private lazy var bottomView: RSStatusBottomButtonView = RSStatusBottomButtonView()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = UIColor(white: 0.98, alpha: 1)
        
        setuoStatusCell()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setuoStatusCell(){
        
        // 添加到父控件中
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        originalView.snp_makeConstraints { (make) in
            make.left.equalTo(contentView).offset(0)
            make.top.equalTo(contentView).offset(10)
            make.right.equalTo(contentView).offset(0)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom).offset(8)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        
        
        bottomView.snp_makeConstraints { (make) in
            make.left.equalTo(contentView)
            self.bottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
//            make.top.equalTo(retweetView.snp_bottom).constraint
            make.right.equalTo(contentView)
            make.height.equalTo(30)
        }
        
        
        // 设置cell的约束, 这也是自动计算行高里最关键的约束
        contentView.snp_makeConstraints { (make) in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
            make.leading.equalTo(self)

        }
    
    }
    
    

}
